package icangyu.base.views;

import android.content.Context;
import androidx.annotation.DrawableRes;
import androidx.annotation.Nullable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import icangyu.base.R;

import java.lang.ref.WeakReference;

public class ToastUtils {

    private static WeakReference<Toast> last;
    private static WeakReference<View> textView;
    private static WeakReference<View> imgView;

    public static void showToast(Context context, String s) {
        if(context == null) return;
        Toast toast = Toast.makeText(context.getApplicationContext(), s, Toast.LENGTH_SHORT);
        WeakReference<Toast> ref= new WeakReference<>(toast);
        hideToast(ref);
        toast.show();
    }

//    public static void showImageToast(Context context, @DrawableRes int resource, String content){
//        if(context == null) return;
//        WeakReference<Toast> toast = ImageToast.showToast(context.getApplicationContext(), resource, content);
//        hideToast(toast);
//        if(toast!=null && toast.get()!=null)
//            toast.get().show();
//    }

    public static void showImageToast(Context context, @DrawableRes int resource, String content){
        if(context == null) return;
//        hideToast(null);
        WeakReference<View> viewRef = getImageView(context.getApplicationContext(), resource, content);
        Toast toast = getToast(context);
        if(viewRef.get()!=null && toast!=null){
            View view =viewRef.get();
            ImageView img = view.findViewById(R.id.img_circle);
            TextView text = view.findViewById(R.id.tv_msg);
            if(img!=null)
                img.setImageResource(resource);
            if(text !=null)
                text.setText(content);
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.setView(view);
            toast.show();
        }else {
            showToast(context,content);
        }
    }

    public static void showTextToast(Context context,String content,int duration){
        if(context == null) return;
//        hideToast(null);
        WeakReference<View> viewRef = getTextView(context.getApplicationContext(),content);
        Toast toast = getToast(context);
        if(viewRef.get()!=null && toast!=null){
            View view =viewRef.get();
            TextView text = view.findViewById(R.id.tvMessage);
            if(text!=null)
                text.setText(content);
            toast.setGravity(Gravity.BOTTOM, 0, 0);
            toast.setView(view);
            toast.show();
        }else {
            showToast(context,content);
        }
    }

    private static WeakReference<View> getTextView(Context context, String message){
        if(context == null) return null;
        if(textView== null || textView.get() == null){
            View v = LayoutInflater.from(context).inflate(R.layout.toast_text, null);
            textView = new WeakReference<>(v);
        }
        return textView;
    }

    private static WeakReference<View> getImageView(Context context, @DrawableRes int d, String message){
        if(context == null) return null;
        if(imgView == null || imgView.get() == null){
            View v = LayoutInflater.from(context).inflate(R.layout.img_toast_layout, null);
            imgView = new WeakReference<>(v);
        }
        return imgView;
    }

//    public static void showTextToast(Context context,String content,int duration){
//        if(context == null) return;
//        WeakReference<Toast> t =TextToast.showToast(context.getApplicationContext(),content,duration);
//        hideToast(t);
//        if(t!=null && t.get()!=null)
//            t.get().show();
//    }

    private static Toast getToast(Context context){
//        if(last == null || last.get()==null || view != last.get().getView()){
            Toast t = new Toast(context.getApplicationContext());
            last = new WeakReference<>(t);
//        }
        return last.get();
    }

    public static void hideToast(@Nullable WeakReference<Toast> toast){
        if (last != null && last.get()!=null)
            last.get().cancel();
        last = toast;
    }

}
